package com.li.net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Socket 允许程序员将网络看作是另外一个可以读写字节的流。Socket对程序员掩盖了网络底层的细节
 * 如错误检测，包大小，包分解，包重传，网络地址等
 * Socket是两台主机之间的一个连接，他可以完成七个操作
 * 1、连接远程机器
 * 2、发送数据
 * 3、接收数据
 * 4、关闭连接
 * 5、绑定端口
 * 6、监听入站端口
 * 7、绑定端口上接接收远程机器的连接
 * Java中客户端的Socket实现了上面四个操作的方法
 * @author Administrator
 *
 */
public class SocketDemo {
	public static void main(String[] args) throws Exception{
		testSocket3();
	}
	
	public static void client() throws IOException{
		//InetAddress inetSocketAddress=InetAddress.getByName("localhost");
		Socket socket=new Socket("localhost",8888);
		socket.setSoTimeout(15000);
		OutputStream os=socket.getOutputStream();
		PrintWriter pw=new PrintWriter(os);
		pw.write("你们还好吗");
		pw.flush();
		//socket.shutdownOutput();
		InputStream is=socket.getInputStream();
		BufferedReader br=new BufferedReader(new InputStreamReader(is));
		char[] data=new char[1024];
		while(br.read(data)!=-1){
			System.out.println(data);
		}
		socket.close();
	}
	
	public static void testSocket() throws Exception{
		//1、新建一个socket，并绑定响应的远程地址和端口，建立起一个连接
		Socket socket = new Socket("time.nist.gov",13);
		//Socket socket = new Socket();
		//socket地址是存储地址+端口的类，可以通过getRemoteAdress获取远程服务器的地址，getLocalhost获取本地主机的地址
		//InetSocketAddress inetSocketAddress= new InetSocketAddress("time.nist.gov",13);
		//connet方式默认连接超时时间为0，即用不超时
		//socket.connect(inetSocketAddress,15000);
		//InetAddress inetAddress=InetAddress.getByName("time.nist.gov");
		//Socket socket = new Socket(inetAddress,13);
		//设置一个socket连接的超时时间，防止客户端一直处于等待建立连接的状态
		socket.setSoTimeout(15000);
		//2、获取一个输出流，向输出流写入数据，发送到远程主机中
		//3、获取一个输入流，接收远程主机返回的响应信息
		InputStream is=socket.getInputStream();
		InputStreamReader ir=new InputStreamReader(is,"ASCII");
		int data=0;
		StringBuilder result=new StringBuilder();
		while((data=ir.read())!=-1){
			result.append((char)data);
		}
		//半关闭socket，只关闭socket的输入流
		//socket.shutdownInput();
		System.out.print(result.toString());
		//4、关闭socket,会同时关闭socket中的输入流和输出流，并使用相应的资源和端口占用
		socket.close();
	}
	
	public static void testSocket2() throws Exception{
		Socket socket = new Socket("www.baidu.com",80);
		System.out.println(socket.getInetAddress());
		System.out.println(socket.getPort());
		System.out.println(socket.getLocalAddress());
		System.out.println(socket.getLocalPort());
		socket.close();
	}
	
	public static void testSocket3() throws Exception{
		Socket socket=new Socket("127.0.0.1",8088);
		OutputStream os=socket.getOutputStream();
		BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
		BufferedReader br=new BufferedReader(new FileReader("src/main/java/com/li/net/1.txt"));
		char[] data=new char[10];
		while(br.read(data)!=-1){
			bw.write(data);
			Thread.sleep(2000);
		}
		bw.flush();
		socket.close();
	}
}
